﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using JFnetcore.Models;
using Utility;
using Microsoft.AspNetCore.Http;
using System.Net.Http.Headers;
using System.IO;
using System.Text;
using static Utility.EnumUtil;
using Services.IService;
using Models;
using System.Linq.Expressions;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JFnetcore.Controllers
{
    public class WikiController : BaseController
    {
        private ILogService _logService;
        private IArticleService _articleService;
        private IArticletypeService _articletypeService;
        private IReplyService _replyService;
        private IUsersService _usersService;
        private IConfigService _configService;
        private IVarticleService _varticleService;
        private IVreplyService _vreplyService;
        public WikiController(
            ILogService logService,
            IArticleService ArticleService,
            IReplyService ReplyService,
            IUsersService UserService,
            IArticletypeService ArticleTypeService,
            IConfigService ConfigService,
            IVarticleService varticleService,
            IVreplyService vreplyService)
        {
            _logService = logService;
            _articleService = ArticleService;
            _replyService = ReplyService;
            _usersService = UserService;
            _articletypeService = ArticleTypeService;
            _configService = ConfigService;
            _varticleService = varticleService;
            _vreplyService = vreplyService;
        }

        #region 金帆论坛

        #region 论坛首页

        /// <summary>
        /// 打开论坛首页视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Main()
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                Expression<Func<VarticleModel, bool>> where = LinqUtil.True<VarticleModel>();
                where = where.AndAlso(i => i.ArticleType_Id == 1 && i.Disable == 1);
                ViewBag.Modellist = _varticleService.GetPageList(where, 1, 10, "Createtime");
                Expression<Func<VarticleModel, bool>> where2 = LinqUtil.True<VarticleModel>();
                where2 = where2.AndAlso(i => i.ArticleType_Id == 2 && i.Disable == 1);
                ViewBag.Modellist2 = _varticleService.GetPageList(where2, 1, 10, "Createtime");
                Expression<Func<VarticleModel, bool>> where3 = LinqUtil.True<VarticleModel>();
                where3 = where3.AndAlso(i => i.ArticleType_Id == 3 && i.Disable == 1);
                ViewBag.Modellist3 = _varticleService.GetPageList(where3, 1, 10, "Createtime");
                return View();

            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }

        #endregion

        #region 文章列表
        /// <summary>
        /// 打开论坛文章列表视图
        /// </summary>
        /// <param name="pagemodel"></param>
        /// <returns></returns>
        public IActionResult List(PageModel pagemodel)
        {
            //获取Session信息并且返回前台
            UsersModel userSession = getUserSession().User;
            if (userSession != null)
            {

                if (string.IsNullOrEmpty(pagemodel.PageCurrent))
                    pagemodel.PageCurrent = "1";
                if (string.IsNullOrEmpty(pagemodel.PageSize))
                    pagemodel.PageSize = "30";
                ViewBag.SearchKeyWords = pagemodel.SearchKeyWords;
                ViewBag.PageSize = pagemodel.PageSize;
                ViewBag.PageCurrent = pagemodel.PageCurrent;
                ViewBag.OrderField = pagemodel.OrderField;
                ViewBag.OrderDirection = pagemodel.OrderDirection;
                ViewBag.ArticleTypeId = pagemodel.ArticleTypeId;
                if (string.IsNullOrEmpty(pagemodel.ArticleTypeId))
                    pagemodel.ArticleTypeId = "0";
                ViewBag.ArticleTypeName = pagemodel.ArticleTypeName;

                Expression<Func<VarticleModel, bool>> where = LinqUtil.True<VarticleModel>();
                if (pagemodel.ArticleTypeId != "0")
                    where = where.AndAlso(i => (i.Article_name.Contains(pagemodel.SearchKeyWords) || i.Article_key.Contains(pagemodel.SearchKeyWords) || i.ArticleType_name.Contains(pagemodel.SearchKeyWords) || i.User_name.Contains(pagemodel.SearchKeyWords)) && i.Disable == 1 && i.ArticleType_Id.ToString() == pagemodel.ArticleTypeId);
                else
                    where = where.AndAlso(i => (i.Article_name.Contains(pagemodel.SearchKeyWords) || i.Article_key.Contains(pagemodel.SearchKeyWords) || i.ArticleType_name.Contains(pagemodel.SearchKeyWords) || i.User_name.Contains(pagemodel.SearchKeyWords)) && i.Disable == 1);
                ViewBag.AllRecords = _varticleService.GetList(where).Count;
                ViewBag.Modellist = _varticleService.GetPageList(where, int.Parse(pagemodel.PageCurrent), int.Parse(pagemodel.PageSize), pagemodel.OrderField);
                return View();

            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }

        /// <summary>
        /// 打开文章内容视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Article(string id)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                var model = _articleService.GetById(Int32.Parse(id));
                string replyvalue = _configService.GetById(2).Config_value;
                ViewBag.Replys = GetReplyTree(id, "0", replyvalue);
                ViewBag.Replaypower = replyvalue;
                return View(model);
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }

        /// <summary>
        /// 表单提交文章回复接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Reply(ReplyModel model)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                if (_configService.GetById(2).Config_value == "0")
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败,论坛回复功能已经关闭！", "", "", "", false, "", ""));
                }
                else
                {
                    model.User_Id = sm.User.Id;
                    model.Createtime = DateTime.Now;
                    if (_replyService.Update(model))
                    {
                        _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.add, "回复文章:" + _articleService.GetById(model.Article_Id).Article_name);
                        return Content(ReturnJsonHelp.BJUIajaxJson(200, "提交成功！", "WikiMain,WikiIndex,WikiList,WikiMylist,WikiArticle", "", "", false, "", ""));
                    }
                    else
                    {
                        return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败！", "", "", "", false, "", ""));
                    }
                }
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }

        /// <summary>
        /// 递归获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetReplyTree(string Articleid, string pid, string replyvalue)
        {
            Expression<Func<VreplyModel, bool>> where = LinqUtil.True<VreplyModel>();
            where = where.AndAlso(i => i.Article_Id.ToString() == Articleid && i.Parent_Id.ToString() == pid);

            List<VreplyModel> list = _vreplyService.GetList(where);
            int x = 1;
            string replystr = "";
            foreach (VreplyModel i in list)
            {
                replystr += "<div class=\"bs-callout bs-callout-warning\">";
                if (i.Parent_Id == 0)
                    replystr += "#" + x + " 楼 ";
                replystr += i.Createtime.ToString("yyyy -MM-dd HH:ss") + " " + i.User_name + "  ";
                if (i.User_Id != getUserSession().User.Id && replyvalue != "0")
                    replystr += " <button type=\"button\" class=\"btn-blue\" onclick=\"huifu('" + i.Id + "','" + i.Reply_txt + "','" + i.User_name + "')\">回复</button>";
                replystr += "    <div class=\"alert alert-success\" role=\"alert\" style=\"margin:0 0 5px; padding:5px 15px; \">";
                replystr += i.Reply_txt;
                replystr += "    </div>";
                replystr += GetReplyTree(Articleid, i.Id.ToString(), replyvalue);
                replystr += "</div>";
                if (i.Parent_Id == 0)
                    x++;
            }
            return replystr.ToString();
        }
        #endregion

        #endregion

        #region 我的空间

        #region 最新动态
        /// <summary>
        /// 打开个人空间最新动态视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {

                return View();

            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }
        #endregion

        #region 发布文章
        /// <summary>
        /// 打开文章编辑视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Edit(string id)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                if (string.IsNullOrEmpty(id))
                {
                    return View();
                }
                else
                {
                    var model = _articleService.GetById(Int32.Parse(id));
                    return View(model);
                }

            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }

        /// <summary>
        /// 表单提交文章编辑接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("WikiEdit")]
        public IActionResult WikiEdit(ArticleModel model)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                if (model.Id == 0)
                {
                    model.User_Id = getUserSession().User.Id;
                    DateTime n = DateTime.Now;
                    model.Createtime = n;
                    model.Admin_Createtime = n;
                    model.Admin_step = getStep();


                    if (_articleService.Insert(model) > 0)
                    {
                        _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.add, "新增文章:" + model.Article_name);
                        return Content(ReturnJsonHelp.BJUIajaxJson(200, "新增成功！", "WikiMain,WikiIndex,WikiList,WikiMylist", "", "", true, "", ""));
                    }
                    else
                    {
                        return Content(ReturnJsonHelp.BJUIajaxJson(300, "新增失败！", "", "", "", false, "", ""));
                    }
                }
                else
                {
                    ArticleModel m = _articleService.GetById(model.Id);
                    m.User_Id = getUserSession().User.Id;
                    DateTime n = DateTime.Now;
                    m.Createtime = n;
                    m.Admin_Createtime = n;
                    if (m.Admin_step == 3)
                        m.Admin_step = 4;
                    else
                        m.Admin_step = getStep();
                    m.Article_key = model.Article_key;
                    m.Article_name = model.Article_name;
                    m.Article_txt = model.Article_txt;
                    m.Disable = model.Disable;
                    m.Disabledesc = model.Disabledesc;
                    m.ArticleType_Id = model.ArticleType_Id;

                    model = m;


                    if (_articleService.Update(model))
                    {
                        _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.update, "修改文章:" + model.Article_name);
                        return Content(ReturnJsonHelp.BJUIajaxJson(200, "修改成功！", "WikiMain,WikiIndex,WikiList,WikiMylist", "", "", true, "", ""));
                    }
                    else
                    {
                        return Content(ReturnJsonHelp.BJUIajaxJson(300, "修改失败！", "", "", "", false, "", ""));
                    }
                }

            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }

        /// <summary>
        /// 配置表获取参数，返回  提交文章后的状态值
        /// </summary>
        /// <param name="step"></param>
        /// <returns></returns>
        private int getStep()
        {
            ConfigModel modle = _configService.GetById(1);
            if (modle != null)
            {
                if (modle.Config_value == "1")
                {
                    return 1;
                }
                else
                {
                    return 2;
                }
            }
            else
            {
                return 2;
            }
        }

        /// <summary>
        /// Kindeditor图片上传
        /// </summary>
        /// <param name="imgFile">Kindeditor图片上传自带的命名，不可更改名称</param>
        /// <param name="dir">不可更改名称 这里没有用到dir</param>
        /// <returns></returns>
        public IActionResult KindeditorPicUpload(IList<IFormFile> imgFile, string dir)
        {
            SessionModel sm = getUserSession();
            PicUploadResponse rspJson = new PicUploadResponse() { error = 0, url = "/upload/" };
            if (sm != null)
            {
                long size = 0;
                string tempname = "";
                foreach (var file in imgFile)
                {
                    var filename = ContentDispositionHeaderValue
                                    .Parse(file.ContentDisposition)
                                    .FileName
                                    .Trim('"');
                    var extname = filename.Substring(filename.LastIndexOf("."), filename.Length - filename.LastIndexOf("."));
                    var filename1 = System.Guid.NewGuid().ToString() + extname;
                    tempname = filename1;
                    filename = System.IO.Directory.GetCurrentDirectory() + "/wwwroot/KEupload/" + filename1;
                    size += file.Length;
                    using (FileStream fs = System.IO.File.Create(filename))
                    {
                        file.CopyTo(fs);
                        fs.Flush();
                        //这里是业务逻辑
                    }
                }
                rspJson.error = 0;
                rspJson.url = "/KEupload/" + tempname;
                return Json(rspJson);
            }
            else
            {
                rspJson.error = 1;
                rspJson.url = "Session超时";
                return Json(rspJson);
            }
        }

        /// <summary>
        /// 文章编辑视图，加载类型选择视图
        /// </summary>
        /// <returns></returns>
        public IActionResult SearchType()
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                StringBuilder resultstr = new StringBuilder();
                List<ArticletypeModel> list = _articletypeService.GetList().Where(i => i.Disable == 1).ToList();
                resultstr.Append("[");
                int y = 0;
                foreach (ArticletypeModel a in list)
                {
                    resultstr.Append("{\"ArticleType_id\":\"" + a.Id + "\",\"ArticleType_name\":\"" + a.ArticleType_name + "\"},");
                    y++;
                }
                if (y > 0)
                    resultstr.Remove(resultstr.ToString().LastIndexOf(','), 1);
                resultstr.Append("]");
                return Content(resultstr.ToString());
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }
        #endregion

        #region 我的文章
        /// <summary>
        /// 打开个人文章列表视图
        /// </summary>
        /// <param name="pagemodel"></param>
        /// <returns></returns>
        public IActionResult Mylist(PageModel pagemodel)
        {
            //获取Session信息并且返回前台
            UsersModel userSession = getUserSession().User;
            if (userSession != null)
            {

                if (string.IsNullOrEmpty(pagemodel.PageCurrent))
                    pagemodel.PageCurrent = "1";
                if (string.IsNullOrEmpty(pagemodel.PageSize))
                    pagemodel.PageSize = "30";
                ViewBag.SearchKeyWords = pagemodel.SearchKeyWords;
                ViewBag.PageSize = pagemodel.PageSize;
                ViewBag.PageCurrent = pagemodel.PageCurrent;
                ViewBag.OrderField = pagemodel.OrderField;
                ViewBag.OrderDirection = pagemodel.OrderDirection;


                Expression<Func<VarticleModel, bool>> where = LinqUtil.True<VarticleModel>();
                where = where.AndAlso(i => (i.Article_name.Contains(pagemodel.SearchKeyWords) || i.Article_key.Contains(pagemodel.SearchKeyWords) || i.ArticleType_name.Contains(pagemodel.SearchKeyWords) || i.User_name.Contains(pagemodel.SearchKeyWords)) && i.User_Id == userSession.Id);
                ViewBag.AllRecords = _varticleService.GetList(where).Count;
                ViewBag.Modellist = _varticleService.GetPageList(where, int.Parse(pagemodel.PageCurrent), int.Parse(pagemodel.PageSize), pagemodel.OrderField);
                return View();

            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }
        /// <summary>
        /// 按钮删除文章接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult ArticleDelete(string id)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                string name = _articleService.GetById(int.Parse(id)).Article_name;
                if (_articleService.DeleteById(int.Parse(id)))
                {
                    _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.delete, "删除文章:" + name);
                    return Content(ReturnJsonHelp.BJUIajaxJson(200, "删除成功！", "WikiMain,WikiIndex,WikiList,WikiMylist", "", "", false, "", ""));
                }
                else
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "删除失败！", "", "", "", false, "", ""));
                }
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }

        #endregion

        #endregion

        #region 论坛管理

        #region 类型管理
        /// <summary>
        /// 打开类型列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ArticleType(PageModel pagemodel)
        {
            //获取Session信息并且返回前台
            UsersModel userSession = getUserSession().User;
            if (userSession != null)
            {

                if (string.IsNullOrEmpty(pagemodel.PageCurrent))
                    pagemodel.PageCurrent = "1";
                if (string.IsNullOrEmpty(pagemodel.PageSize))
                    pagemodel.PageSize = "30";
                ViewBag.SearchKeyWords = pagemodel.SearchKeyWords;
                ViewBag.PageSize = pagemodel.PageSize;
                ViewBag.PageCurrent = pagemodel.PageCurrent;
                ViewBag.OrderField = pagemodel.OrderField;
                ViewBag.OrderDirection = pagemodel.OrderDirection;

                Expression<Func<ArticletypeModel, bool>> where = LinqUtil.True<ArticletypeModel>();
                where = where.AndAlso(i => i.ArticleType_name.Contains(pagemodel.SearchKeyWords));
                ViewBag.AllRecords = _articletypeService.GetList(where).Count;
                ViewBag.Modellist = _articletypeService.GetPageList(where, int.Parse(pagemodel.PageCurrent), int.Parse(pagemodel.PageSize), pagemodel.OrderField);
                return View();

            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }
        /// <summary>
        /// 按钮提交类型删除接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult ArticleTypeDelete(string id)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                if (_articleService.GetList().Where(i => i.ArticleType_Id.ToString() == id).Count() > 0)
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "删除失败,该类型下有文章！", "", "", "", false, "", ""));
                }
                else
                {
                    string name = _articletypeService.GetById(int.Parse(id)).ArticleType_name;
                    if (_articletypeService.DeleteById(int.Parse(id)))
                    {
                        _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.delete, "删除文章类型:" + name);
                        return Content(ReturnJsonHelp.BJUIajaxJson(200, "删除成功！", "WikiArticleType", "", "", false, "", ""));
                    }
                    else
                    {
                        return Content(ReturnJsonHelp.BJUIajaxJson(300, "删除失败！", "", "", "", false, "", ""));
                    }
                }
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }
        /// <summary>
        /// 打开类型编辑视图，添加类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult ArticleTypeEdit(string id)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                if (string.IsNullOrEmpty(id))
                {
                    return View();
                }
                else
                {
                    var model = _articletypeService.GetById(Int32.Parse(id));
                    return View(model);
                }
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));

        }

        /// <summary>
        ///表单提交类型编辑接口 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("EditArticleType")]
        public IActionResult EditArticleType(ArticletypeModel model)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                model.Createtime = DateTime.Now;
                if (model.Id != 0)
                {
                    if (_articletypeService.Update(model))
                    {
                        _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.update, "修改文章类型 " + model.ArticleType_name);
                        return Content(ReturnJsonHelp.BJUIajaxJson(200, "修改文章类型 " + model.ArticleType_name + " 成功！", "WikiArticleType", "", "", true, "", ""));
                    }
                    else
                    {
                        return Content(ReturnJsonHelp.BJUIajaxJson(300, "修改文章类型 " + model.ArticleType_name + " 失败！", "", "", "", false, "", ""));
                    }
                }
                else
                {
                    if (_articletypeService.Insert(model) > 0)
                    {
                        _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.add, "新增文章类型 " + model.ArticleType_name);
                        return Content(ReturnJsonHelp.BJUIajaxJson(200, "新增文章类型 " + model.ArticleType_name + " 成功！", "WikiArticleType", "", "", true, "", ""));
                    }
                    else
                    {
                        return Content(ReturnJsonHelp.BJUIajaxJson(300, "新增文章类型 " + model.ArticleType_name + " 失败！", "", "", "", false, "", ""));
                    }
                }
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }
        #endregion

        #region 审核管理
        /// <summary>
        /// 打开论坛管理文章管理列表视图
        /// </summary>
        /// <param name="pagemodel"></param>
        /// <returns></returns>
        public IActionResult AdminList(PageModel pagemodel)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {

                if (string.IsNullOrEmpty(pagemodel.PageCurrent))
                    pagemodel.PageCurrent = "1";
                if (string.IsNullOrEmpty(pagemodel.PageSize))
                    pagemodel.PageSize = "30";
                ViewBag.SearchKeyWords = pagemodel.SearchKeyWords;
                ViewBag.PageSize = pagemodel.PageSize;
                ViewBag.PageCurrent = pagemodel.PageCurrent;
                ViewBag.OrderField = pagemodel.OrderField;
                ViewBag.OrderDirection = pagemodel.OrderDirection;

                Expression<Func<VarticleModel, bool>> where = LinqUtil.True<VarticleModel>();
                where = where.AndAlso(i => i.Article_name.Contains(pagemodel.SearchKeyWords) || i.Article_key.Contains(pagemodel.SearchKeyWords) || i.ArticleType_name.Contains(pagemodel.SearchKeyWords) || i.User_name.Contains(pagemodel.SearchKeyWords));
                ViewBag.AllRecords = _varticleService.GetList(where).Count;
                ViewBag.Modellist = _varticleService.GetPageList(where, int.Parse(pagemodel.PageCurrent), int.Parse(pagemodel.PageSize), pagemodel.OrderField);
                return View();

            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }
        /// <summary>
        /// 打开文章驳回，驳回建议视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AdminBack(string id)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                var model = _articleService.GetById(Int32.Parse(id));
                return View(model);
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }

        /// <summary>
        /// 按钮审核通过
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AdminSuccess(string id)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                var model = _articleService.GetById(Int32.Parse(id));
                model.Admin_Createtime = DateTime.Now;
                model.Admin_Id = getUserSession().User.Id;
                model.Admin_step = 2;
                if (_articleService.Update(model))
                {
                    _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.success, "审核通过文章:" + model.Article_name);
                    return Content(ReturnJsonHelp.BJUIajaxJson(200, "提交成功！", "WikiAdminList", "", "", false, "", ""));
                }
                else
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败！", "", "", "", false, "", ""));
                }
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));

        }

        /// <summary>
        /// 表单提交文章驳回接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("AdminBack")]
        public IActionResult AdminBack(ArticleModel model)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                ArticleModel m = _articleService.GetById(model.Id);
                m.Admin_Id = getUserSession().User.Id;
                m.Admin_Createtime = DateTime.Now;
                m.Admin_remark = model.Admin_remark;
                m.Admin_step = 3;
                if (_articleService.Update(m))
                {
                    _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.back, "驳回文章:" + m.Article_name);
                    return Content(ReturnJsonHelp.BJUIajaxJson(200, "提交成功！", "WikiAdminList", "", "", true, "", ""));
                }
                else
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败！", "", "", "", false, "", ""));
                }
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));

        }


        #endregion

        #region 参数管理
        /// <summary>
        /// 打开参数设置视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Configs()
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {

                ViewBag.audit = _configService.GetById(1).Config_value;
                ViewBag.replay = _configService.GetById(2).Config_value;
                return View();
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));

        }

        /// <summary>
        /// 修改参数接口
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        [Route("Wiki/ConfigUpdate/{key?}/{value?}")]
        public IActionResult ConfigUpdate(string key, string value)
        {
            SessionModel sm = getUserSession();
            if (sm != null)
            {
                ConfigModel model = _configService.GetById(int.Parse(key));
                model.Config_value = value;
                if (_configService.Update(model))
                {
                    _logService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)EnumUtil.OperationEnum.update, "修改参数[" + model.Config_name + "]为:" + value);
                    return Content(ReturnJsonHelp.BJUIajaxJson(200, "提交成功！", "Configs", "", "", false, "", ""));
                }
                else
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败！", "Configs", "", "", false, "", ""));
                }
            }
            else
                return Content(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
        }
        #endregion

        #endregion


    }
}
